library(plotly)month <-c('January', 'February', 'March', 'April', 'May', 'June', 'July','August', 'September', 'October', 'November', 'December')high_2014 <-c(28.8, 28.5, 37.0, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9)low_2014 <-c(12.7, 14.3, 18.6, 35.5, 49.9, 58.0, 60.0, 58.6, 51.7, 45.2, 32.2, 29.1)data <-data.frame(month, high_2014, low_2014)data$average_2014 <-rowMeans(data[,c("high_2014", "low_2014")])#The default order will be alphabetized unless specified as below:data$month <-factor(data$month, levels = data[["month"]])fig <-plot_ly(data, x =~month, y =~high_2014, type ='scatter', mode ='lines',line =list(color ='transparent'),showlegend =FALSE, name ='High 2014') fig <- fig %>%add_trace(y =~low_2014, type ='scatter', mode ='lines',fill ='tonexty', fillcolor='rgba(0,100,80,0.2)', line =list(color ='transparent'),showlegend =FALSE, name ='Low 2014') fig <- fig %>%add_trace(x =~month, y =~average_2014, type ='scatter', mode ='lines',line =list(color='rgb(0,100,80)'),name ='Average') fig <- fig %>%layout(title ="Average, High and Low Temperatures in New York",paper_bgcolor='rgb(255,255,255)', plot_bgcolor='rgb(229,229,229)',xaxis =list(title ="Months",gridcolor ='rgb(255,255,255)',showgrid =TRUE,showline =FALSE,showticklabels =TRUE,tickcolor ='rgb(127,127,127)',ticks ='outside',zeroline =FALSE),yaxis =list(title ="Temperature (degrees F)",gridcolor ='rgb(255,255,255)',showgrid =TRUE,showline =FALSE,showticklabels =TRUE,tickcolor ='rgb(127,127,127)',ticks ='outside',zeroline =FALSE))fig
Gráfica de densidad
Code
dens <-with(diamonds, tapply(price, INDEX = cut, density))df <-data.frame(x =unlist(lapply(dens, "[[", "x")),y =unlist(lapply(dens, "[[", "y")),cut =rep(names(dens), each =length(dens[[1]]$x)))fig <-plot_ly(df, x =~x, y =~y, color =~cut) fig <- fig %>%add_lines()fig
Escalas de colores cualitativas
Code
fig <-plot_ly(data = iris, x =~Sepal.Length, y =~Petal.Length, color =~Species)fig
Nombres de paletas ColorBrewer
Code
fig <-plot_ly(data = iris, x =~Sepal.Length, y =~Petal.Length, color =~Species, colors ="Set1")fig
Mapeo de datos a símbolos
Code
fig <-plot_ly(data = iris, x =~Sepal.Length, y =~Petal.Length, type ='scatter',mode ='markers', symbol =~Species, symbols =c('circle','x','o'),color =I('black'), marker =list(size =10))fig
Agregar asignación de color y tamaño
Code
d <- diamonds[sample(nrow(diamonds), 1000), ]fig <-plot_ly( d, x =~carat, y =~price,color =~carat, size =~carat)fig
Etiquetas de datos al pasar el cursor
Code
d <- diamonds[sample(nrow(diamonds), 1000), ]fig <-plot_ly( d, x =~carat, y =~price,# Hover text:text =~paste("Price: ", price, '$<br>Cut:', cut),color =~carat, size =~carat)fig
Gráficos de barras
Code
x=c("giraffes", "orangutans", "monkeys")y=c(20, 14, 23)fig <-plot_ly(x = x ,y = y ,name ="SF Zoo",type ="bar")fig
Graficos de barras agrupadas
Code
Animals <-c("giraffes", "orangutans", "monkeys")SF_Zoo <-c(20, 14, 23)LA_Zoo <-c(12, 18, 29)data <-data.frame(Animals, SF_Zoo, LA_Zoo)fig <-plot_ly(data, x =~Animals, y =~SF_Zoo, type ='bar', name ='SF Zoo')fig <- fig %>%add_trace(y =~LA_Zoo, name ='LA Zoo')fig <- fig %>%layout(yaxis =list(title ='Count'), barmode ='group')fig
La buena noticia
ggplot2 to plotly
✨Tips ✨ Con ggplotly() de Plotly, puedes convertir tus figuras ggplot2 en figuras interactivas desarrolladas con plotly.js